package com.controller;

import com.dao.IGoodDao;
import com.domain.Good;
import com.service.IGoodService;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/good")
public class GoodController {

    @Autowired
    private IGoodService goodService;

    @RequestMapping("/findOneGood")
    public ModelAndView findOneGood(Good good1){
        ModelAndView modelAndView=new ModelAndView();
        Good good=goodService.findOneGood(good1);
        modelAndView.addObject("good",good);
        modelAndView.setViewName("goodInf");
        return modelAndView;
    }

    @RequestMapping("/addOneGood")
    public String addOneGood(Good good1, HttpServletRequest request){
        Good good=goodService.findOneGood(good1);
        HttpSession session=request.getSession();
        List<Good> goods=(List<Good>) session.getAttribute("shoppingCar");
        if (goods==null){
            goods=new ArrayList<Good>();
        }
        goods.add(good);
        session.setAttribute("shoppingCar",goods);
        return "redirect:/user/goodList";
    }

    @RequestMapping("/shoppingCar")
    public String shoppingCar(){
        return "shoppingCar";
    }

    @RequestMapping("/findGoodByType")
    public ModelAndView findGoodByType(Good good){
        ModelAndView modelAndView=new ModelAndView();
        List<Good> goods=goodService.findGoodByType(good);
        modelAndView.addObject("goods",goods);
        modelAndView.setViewName("navi");
        return modelAndView;
    }


}
